package com.vcredit.bird.service.emall;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.creditcat.enumtype.BirdResponeEnum;
import com.vcredit.creditcat.exception.BirdRequestException;
import com.vcredit.bird.service.base.BirdResponse;
import com.vcredit.bird.service.base.BirdService;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;

@Service
public class TaobaoProcessService extends BirdService {
	@Value("${taobao.bill.init}")
	private String TAOBAO_BILL_INIT;

	@Value("${taobao.bill.login}")
	private String TAOBAO_BILL_LOGIN;

	@Value("${taobao.bill.crawlstatus}")
	private String TAOBAO_BILL_CRAWLSTATUS;

	@Value("${taobao.bill.isAuth}")
	private String TAOBAO_BILL_ISAUTH;

	@Log
	public BirdResponse taobaoInit() throws Exception {
		String result = HttpRequest.doPost(TAOBAO_BILL_INIT, new String[] {});
		BirdResponse response = JacksonUtil.Json2Obj(result, new TypeReference<BirdResponse>() {
		});
		if (response.getCode().equals(BirdResponeEnum.HttpRequestSuccess.getCode())) {
			return response;
		} else {
			throw new BirdRequestException("淘宝初始化失败:" + response.getDesc());
		}
	}

	@Log
	public BirdResponse taobaoLogin(String userName, String password, String name, String identityCard, String cookies,
			String token, String bustype) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("Token", token);
		map.put("Cookies", cookies);
		map.put("BusType", bustype);
		map.put("Username", userName);
		map.put("Password", password);
		map.put("IdentityCard", identityCard);
		map.put("Name", name);
		try {
			return super.postBirdData(TAOBAO_BILL_LOGIN, JacksonUtil.Obj2Json(map));
		} catch (Exception e) {
			e.printStackTrace();
			throw new BirdRequestException("淘宝登陆异常:" + e.getMessage());
		}
	}

	@Log
	public BirdResponse taobaoCrawlStatus(String token) throws Exception {
		String param = "{\"Token\":\"" + token + "\"}";
		try {
			return super.postBirdData(TAOBAO_BILL_CRAWLSTATUS, param);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BirdRequestException("根据token查询淘宝采集状态异常:" + e.getMessage());
		}
	}

	@Log
	public boolean TaobaoIsAuth(String token, String name, String mobile, String identityCard) throws Exception {
		String param = "{\"Token\":\"" + token + "\",\"Name\":\"" + name + "\",\"Mobile\":\"" + mobile
				+ "\",\"IdentityCard\":\"" + identityCard + "\"}";
		BirdResponse response = null;
		try {
			response = super.postBirdData(TAOBAO_BILL_ISAUTH, param);
			return Boolean.valueOf(response.getResult());
		} catch (Exception e) {
			e.printStackTrace();
			throw new BirdRequestException("根据token查询淘宝实名信息异常:" + e.getMessage());
		}
	}

}
